package com.quanyan.stadium.service.commonComponent.impl;

import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.stadium.biz.ConfigDao;
import com.quanyan.stadium.entity.TbStadiumProperty;
import com.quanyan.stadium.service.commonComponent.ConfigService;
import com.quanyan.stadium.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/21.
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    private static Map<String, String> configMap = new HashMap<>();

    @Autowired
    private ConfigDao configDao;

    @Override
    public String getProperty(String key, String defaultValue) {
        if (StringUtils.isEmpty(configMap.get(key))) {
            loadingData(key);
        }

        return StringUtils.isEmpty(configMap.get(key)) ? defaultValue : configMap.get(key).trim();
    }

    @Override
    public boolean getProperty(String key, boolean defaultValue) {

        return StringUtils.isEmpty(configMap.get(key)) ? defaultValue: Boolean.parseBoolean(configMap.get(key));
    }

    private void loadingData(String key) {
        try {

            TbStadiumProperty tbStadiumProperty = configDao.selectConfigInfo(key);
            if (ObjectUtil.isNotObjectEmpty(tbStadiumProperty) && StringUtils.isNotEmpty(tbStadiumProperty.getValue())) {
                configMap.put(key, tbStadiumProperty.getValue());
            }
        } catch (Exception e) {

        }
    }
}
